<?php defined('BASEPATH') OR exit('No direct script access allowed');
/**
 * Description: 商品模型
 *
 * @author      Eden
 * @datetime    2016/12/14 20:27
 * @copyright   Beijing CmsTop Technology Co.,Ltd.
 */

class Category_Model extends ADM_Model
{

    /**
     * 初始化
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 修改分类
     * @param $params
     * @return bool
     */
    public function resetCats($params)
    {
        $id = $params['catId'];
        $data = array();
        if (!empty($params['name'])) {
            $data['name'] = $params['name'];
        }
        if (isset($params['sort'])) {
            $data['sort'] = intval($params['sort']);
        }

        try {
            $this->db->where('id', $id);
            $this->db->update('base_category', $data);
            if ($this->db->affected_rows()) {
                $q = $this->db->get_where('base_category', array('id' => $id));
                $row = $q->row_array();
                if ($row) {
                    $data['id'] = intval($row['id']);
                    $data['name'] = $row['name'];
                    $data['sort'] = intval($row['sort']);
                    $data['parentId'] = intval($row['parentId']);
                    return $data;
                }
            }
        } catch(PDOException $e) {}
        return false;
    }

    /**
     * 添加分类
     * @param $params
     * @return array|bool
     */
    public function addCat($params, $type = 'parent')
    {
        $data = array(
            'name' => $params['name'],
            'sort' => 0,
            'parentId' => $type == 'parent' ? 0 : intval($params['parentId']),
            'created' => dateTime(),
            'status' => 1
        );
        $this->db->insert('base_category', $data);
        $lastId = $this->db->insert_id();
        if ($lastId) {
            unset($data['created']);
            if ($type == 'parent') {
                $data['subCats'] = [];
            }
            return array_merge($data, array('id' => intval($lastId)));
        }
        return false;
    }

    /**
     * 删除
     * @param $id
     * @return bool
     */
    public function removeCat($id)
    {
        if (!$this->checkIfRemove($id)){
            return false;
        }
        $db = clone($this->db);
        $db->where(array('id' => $id));
        return $db->update('base_category', array('status' => -1));
    }

    /**
     * 是否能删除
     * @param $id
     * @return bool
     */
    private function checkIfRemove($id)
    {
        $db = clone($this->db);
        $q = $db->get_where('base_category', array('id' => $id));
        $row = $q->row_array();
        if (empty($row)){
            $this->setError('分类不存在');
            return false;
        }

        if ($row['parentId'] == 0){
            // 一级
            $pDb = clone($this->db);
            $pDb->where("parentId={$id} and status != -1");
            $q = $pDb->get('base_category');
            if ($q->result_array()){
                $this->setError('该分类含有子分类');
                return false;
            }
            return true;
        } else {
            // 二级
            $q = $this->db->where('categoryId', $id)
                ->where('status != -1');
            if ($q->count_all_results('base_goods')){
                $this->setError('该分类下含有商品');
                return false;
            }
            return true;
        }
    }

}
